package com.shujia.wyh;

import com.shujia.wyh.utils.MySqlTool;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCDemo6 {
    public static void main(String[] args) throws Exception {
        //查询薪资大于3000的员工
        String sql = "select empno,ename,sal from emp where sal>?";
        //获取预编译数据库操作对象
        //通过工具类的方式获取
        PreparedStatement statement = MySqlTool.getStatement(sql);
        statement.setBigDecimal(1, new BigDecimal(3000));
        //执行sql语句
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            String empno = resultSet.getString(1);
            String ename = resultSet.getString(2);
            String sal = resultSet.getString(3);
            System.out.println("员工编号：" + empno + ", 员工姓名：" + ename + ", 员工薪资：" + sal);
        }

        //修改XIAOHU部门编号
        String sql2 = "update emp set deptno=30 where ename='XIAOHU'";
        Statement statement1 = MySqlTool.getStatement();
        int i = statement1.executeUpdate(sql2);
        Connection conn = MySqlTool.getConnection();
        conn.setAutoCommit(false);
        conn.commit();

        System.out.println(i == 1 ? "更新成功！！" : "更新失败！！");

    }
}
